<template>
  <div class="app-container">
    <el-form ref="form" :model="form" inline>
      <el-form-item prop="search">
        <el-input v-model="form.search" clearable style="width:300px" prefix-icon="el-icon-search" placeholder="实例名称、IPV4地址" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-search" size="medium" @click="search(form)">搜索</el-button>
        <el-button type="warning" icon="el-icon-refresh-left" size="medium" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" style="margin-bottom:20px" icon="el-icon-plus" size="medium" @click="createServer">新增</el-button>
    <el-button type="danger" icon="el-icon-delete" @click="deleteServers" >删除</el-button>
    <el-button type="primary"  @click="updateTencentServers" >腾讯云服务器更新</el-button>
    <el-table
      ref="table"
      :data="serverData"
      style="width: 100%; margin-bottom: 20px;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />

      <el-table-column
        fixed
        label="实例名称"
        prop="name"
        width="120"
        show-overflow-tooltip
      />

      <el-table-column
        label="实例ID"
        prop="instance_id"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="内存/G"
        prop="memory"
        width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="CPU/核"
        prop="cpu"
        width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="IPV4"
        prop="ipv4"
        width="120"
        show-overflow-tooltip
      >
        <!--        <template slot-scope="scope">-->
        <!--          <div v-for="ipaddr in scope.row.ipv4">-->
        <!--            {{ ipaddr }}-->
        <!--          </div>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column
        label="ipv6"
        prop="IPv6Addresses"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="公网IP"
        prop="pub_ip"
        width="140"
        show-overflow-tooltip
      >
        <!--        <template slot-scope="scope">-->
        <!--          <div v-for="ipaddr in scope.row.pub_ip" v-if="scope.row.pub_ip != null ">-->
        <!--            {{ ipaddr }}-->
        <!--          </div>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column
        label="状态"
        prop="status"
        width="80"
        show-overflow-tooltip
      />

      <el-table-column
        label="操作系统"
        prop="os"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        prop="create_date"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="过期时间"
        prop="expired_date"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="云平台"
        prop="cloud"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="区域"
        prop="zone"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="Tags"
        prop="tags"
        width="200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.tags" :key="index">
            {{ item }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="归属标志"
        prop="belong_flag"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        fixed="right"
        width="200"
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-edit" size="mini" :disabled="row.cloud != 'private'" @click="updateServer(row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="row.cloud != 'private'" @click="deleteServer(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="1"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <cuForm :dialog-visible="cuDialogVisible" :cur-id="curId" @close="close" @search="search" />
  </div>
</template>

<script>

import { deleteServer, getServers,deleteServers,updateTencentServer  } from '@/api/cmdb/server'
import cuForm from './components/cuForm'

export default {
  name: 'Index',
  components: { cuForm },
  data() {
    return {
      form: {
        page: 1,
        size: 8,
        search: '',
        ordering: 'id'
      },
      total: 0,
      serverData: [],
      multipleSelection: [],
      // 以下为cuForm子组件数据
      cuDialogVisible: false,
      curId: null
    }
  },
  created() {
    this.search()
  },
  methods: {
    //更新腾讯云服务器列表
    updateTencentServers(){
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      updateTencentServer().then(res => {
        if(res.code === 200){
          console.log('更新成功')
          loading.close()
          this.$message({
            message: `更新服务器数量:${res.data.count}`,
            type: 'success'
          });
        }
      }).catch(err => {
        console.log(err)
        loading.close()
        })
    },
  // 批量删除服务器
    deleteServers() {
      this.$confirm('此操作将删除选中服务器, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(this.multipleSelection)
        deleteServers(this.multipleSelection).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          // 刷新table
          this.search()
        })
      })
    },
    // 选项改变时触发
    handleSelectionChange(selectd) {
      const deleteIds = []
      // this.$refs.table.selection.forEach(data => deleteIds.push(data.id))
      selectd.forEach(data => deleteIds.push(data.id))
      this.multipleSelection = deleteIds
      // console.log(val)
      // console.log(this.multipleSelection)
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.search()
    },
    search() {
      getServers(this.form).then(res => {
        this.serverData = res.data.results
        this.form.page = 1
        this.total = res.data.count
      })
    },
    close() {
      this.cuDialogVisible = false
      this.curId = null
    },
    // 分页
    handleSizeChange(val) {
      this.form.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.form.page = val
      this.search()
    },
    // cuForm子组件
    createServer() {
      this.cuDialogVisible = true
    },
    updateServer(row) {
      // console.log(row)
      this.curId = row.id
      this.cuDialogVisible = true
    },
    // 删除
    deleteServer(row) {
      this.$confirm('此操作将删除服务器 "' + row.name + ' , 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteServer(row.id).then(res => {
          this.$message({
            message: '删除服务器' + row.name + '成功',
            type: 'success'
          })
          // 刷新table
          this.search()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
